﻿using BAIDUAPI.Objects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SystemData;

namespace BAIDUAPI
{
    namespace Objects
    {
        public class ResultObject<T>
        {
            public int error_code { get; set; }
            public string error_msg { get; set; }
            public string log_id { get; set; }
            public string timestamp { get; set; }
            public string cached { get; set; }
            public T result { get; set; }
        }
        public class FaseSignResult : ResultObject<FaseSignResult.SignUserFace>
        {
            public class Location
            {
                public string left { get; set; }
                public string top { get; set; }
                public string width { get; set; }
                public string height { get; set; }
                public string rotation { get; set; }
            }

            public class SignUserFace
            {
                public string face_token { get; set; }
                public Location location { get; set; }
            }
        }
        public class FaceLoginResult : ResultObject<FaceLoginResult.LoginUserFace>
        {
            public class User_list
            {
                public string group_id { get; set; }
                public string user_id { get; set; }
                public string user_info { get; set; }
                public float score { get; set; }
            }

            public class LoginUserFace
            {
                public string face_token { get; set; }
                public List<User_list> user_list { get; set; }
            }
        }
    }
    class API
    {
        public static byte[] UrlToByte(string url)
        {
            byte[] arr;
            using (FileStream fs = new FileStream(url, FileMode.Open))
            {
                arr = new byte[fs.Length];
                fs.Read(arr, 0, arr.Length);
                fs.Close();
            }
            return arr;
        }

        public const string APIKEY = "yxImZNKGFdxdvG5YPiQgADCl", SecretKey = "HDaQFP40UCU6UdZytAcVLLufHiilgRK6",GROUP="park",FACE_TOKEN= "ORXW4Z3KMFWWK4Y=";
        public const long SPAN = 60*3;


        public  static CodeInfo CreateCode(String id)
        {
            DataProcess.Authable.TimeBasedOneTimeAuthenticator authenticator = new DataProcess.Authable.TimeBasedOneTimeAuthenticator(Convert.ToBase64String(Encoding.Default.GetBytes(id)), SPAN);
            authenticator.Digit = 9;
            return new CodeInfo() { Token = authenticator.Code, Remaining = authenticator.Remaining.TotalSeconds };
        }

        public    static bool VerifyCode(string code, string id)
        {
            DataProcess.Authable.TimeBasedOneTimeAuthenticator authenticator = new DataProcess.Authable.TimeBasedOneTimeAuthenticator(Convert.ToBase64String(Encoding.Default.GetBytes(id)), SPAN);
            authenticator.Digit = 9;
            return authenticator.VerifyCode(code);
        }

        public FaseSignResult SignUser(byte[] data, string group, string userid)
        {
            Baidu.Aip.Face.Face face = new Baidu.Aip.Face.Face(APIKEY, SecretKey);
            string img;
            face.Timeout = 2000;
            img = Convert.ToBase64String(data);
            return face.UserAdd(img, "BASE64", group, userid).ToObject<FaseSignResult>();
        }
        public FaceLoginResult LoginUser(byte[] data, string group)
        {
            Baidu.Aip.Face.Face face = new Baidu.Aip.Face.Face(APIKEY, SecretKey);
            string img;
            face.Timeout = 2000;
            img = Convert.ToBase64String(data);
            var p = face.Search(img, "BASE64", group);
            return p.ToObject<FaceLoginResult>();
        }

    }
}